package com.itheima.dao.system;

import com.itheima.domain.system.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

/***
 * 平台系统管理 - > 角色管理
 */
public interface RoleDao {
    @Select("select * from ss_role")
    @Results(id = "roleMap",value = {
            @Result(column = "role_id",property = "id",jdbcType = JdbcType.VARCHAR),
            @Result(column = "name",property = "name",jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark",property = "remark",jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time",property = "createTime",jdbcType = JdbcType.TIMESTAMP),
    })
    List<Role> findAll() throws Exception;

    @Select("select * from ss_role where role_id = #{roleId}")
    @ResultMap("roleMap")
    Role findById(String roleId) throws Exception;

    @Insert("INSERT INTO ss_role (role_id, NAME, remark, create_time) VALUES (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})")
    int save(Role role) throws Exception;

    @Update("UPDATE ss_role SET NAME = #{name,jdbcType=VARCHAR}, remark = #{remark,jdbcType=VARCHAR}, create_time = #{createTime,jdbcType=TIMESTAMP} WHERE role_id = #{id,jdbcType=VARCHAR}")
    int update(Role role)throws Exception;

    @Delete("delete from ss_role  where role_id = #{id,jdbcType=VARCHAR}")
    int deleteById(String roleId)throws Exception;


    /**查询模块列表*/
    @Select("SELECT m.`module_id` 'id', m.`parent_id` 'pId', m.`name` 'name', CASE WHEN m.`module_id` IN (SELECT module_id FROM ss_role_module WHERE role_id = #{roleId}) THEN 'true' ELSE 'false' END 'checked' FROM ss_module m")
    List<Map> findAuthorByRoleId(String roleId)throws Exception;

    /**删除某个角色的所有的模块(功能)*/
    @Delete("DELETE FROM ss_role_module WHERE role_id = #{roleId}")
    int deleteAuthorByRoleId(String roleId)throws Exception;

    @Insert("insert into ss_role_module(role_id,module_id) values(#{r},#{m})")
    int saveRoleMoudle(@Param("r") String roleId, @Param("m") String moduleId)throws Exception;

    /**查询所有的角色，用于前端显示树*/
    @Select("SELECT r.`role_id` 'id', r.name 'name', CASE WHEN r.role_id IN (SELECT role_id FROM ss_role_user WHERE user_id=#{userId}) THEN 'true' ELSE 'false' END 'checked' FROM ss_role r ")
    List<Map> findRolesByUserId(String userId)throws Exception;
}
